<template>

  <div>
    <!--搜索重置部分-->
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入比赛名称" suffix-icon="el-icon-search" v-model="contestName"></el-input>
      <el-input style="width: 200px" placeholder="请输入比赛类型" suffix-icon="el-icon-message" class="ml-5" v-model="contestType"></el-input>
      <el-button class="ml-5" type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <!--&lt;!&ndash;发布比赛&ndash;&gt;-->
    <!--<div>-->
    <!--  <el-button style="float:right" type="danger" @click="submitForm('publishForm')">发布比赛</el-button>-->
    <!--</div>-->

    <div style="margin: 10px 0">
      <!--新增-->
      <el-button  type="primary" @click="handleAdd">新增比赛 <i class="el-icon-circle-plus-outline"></i></el-button>
      <!--批量删除-->
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>

<!--      &lt;!&ndash;导入 路径后期改&ndash;&gt;-->
<!--      <el-upload :headers="{Authorization:JSON.parse(localStorage.getItem('user'))}" action="http://localhost:9090/admin/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess"-->
<!--                 :on-error="handleExcelImportError" style="display: inline-block">-->
<!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
<!--      </el-upload>-->

      <!--导出-->
      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <!--表单数据-->
    <!--border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange"-->
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="contestName" label="竞赛名称"></el-table-column>
      <el-table-column prop="organizer" label="主办单位" ></el-table-column>
      <el-table-column prop="contestType" label="竞赛类别" ></el-table-column>
      <el-table-column prop="year" label="年份"></el-table-column>


      <!--修改、删除操作-->
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--页码部分-->
    <!--<div style="padding: 10px 0">-->
    <div style="padding: 10px 0">
      <span class="demonstration"></span>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--新增与修改-->
    <el-dialog title="比赛信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="竞赛名称">
          <el-input v-model="form.contestName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="主办单位">
          <el-input v-model="form.organizer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="竞赛类型">
          <el-select v-model="form.contestType" placeholder="请选择竞赛类型" autocomplete="off">
            <el-option label="国家级" value="国家级"></el-option>
            <el-option label="省级" value="省级"></el-option>
            <el-option label="校级" value="校级"></el-option>
            <el-option label="院级" value="院级"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份">
          <el-input v-model="form.year" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name: 'ContestInfo',
  data() {
    return {
      localStorage: window.localStorage,
      contestName:"",
      contestType:"",
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      multipleSelection: [],
      headerBg: 'headerBg',
      form: {
        contestName:"",
        contestType:"",
        organizer:"",
        year:""

      },
      dialogFormVisible: false
    }
  },
  created() {
    this.load()
  },
  methods: {

    load() {
      this.request.get("/admin/contestinfo/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          contestName: this.contestName,
          contestType: this.contestType,
        }
      }).then(res => {
        console.log(res)

        this.tableData = res.data.records
        this.total = res.data.total

      })
    },
    save() {

      this.request.post("/admin/contestinfo", this.form).then(res => {
        if (res) {
          console.log(res)
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          console.log(res)
          this.$message.error("保存失败")
        }
      }).catch(error=>{
        console.error(error.response)
        this.$message.error("保存失败")
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    del(id) {
      this.request.delete("/admin/contestinfo/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/admin/contestinfo/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.contestName = ""
      this.contestType = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    exp() {
      const token = localStorage.getItem('user');
      const xhr = new XMLHttpRequest();
      xhr.open('GET', 'http://localhost:9090/admin/contestinfo/export');
      xhr.setRequestHeader('Authorization', token);
      xhr.responseType = 'blob';
      xhr.onload = function() {
        const url = window.URL.createObjectURL(xhr.response);
        const a = document.createElement('a');
        a.href = url;
        a.download = '比赛信息.xlsx';
        a.click();
        window.URL.revokeObjectURL(url);
      };
      xhr.send();




    },

    submitForm(formName) {
      this.$router.push("/admin/publishcontest");
    },
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>
